<?php
require_once APPLICATION_PATH . '/controllers/LoginController.php';

/**
 * 合同附件）控制器
 * @author Administrator
 *
 */
class ContractattachmentController extends LoginController
{

    public function init()
    {
    	
    	//删除临时付费项目
    	//$contractAttachmentM = new Application_Model_DbTable_Contractattachment();
        //$contractAttachmentM->deleteTempItem();
        parent::init();
    }

    /**
     * 列表
     * @see LoginController::indexAction()
     */
    public function indexAction()
    {    	
    	$contractAttachmentM = new Application_Model_DbTable_Contractattachment();
                
        if($this->getRequest()->isPost()){
        	//获取数据
	    	$userM  = new Application_Model_DbTable_User();	    	
	    	$userKV = $userM->getKVnameid();
	    	
        	$contractAttachmentM = new Application_Model_DbTable_Contractattachment();
        	$pageM = $this->_helper->Flexgp();
			$data = $contractAttachmentM->jqfetchAll($pageM);
			$rdata= $data["data"];
			foreach ($rdata as &$d){
				$d["create_user"] = $userKV[$d["create_user"]];
				$d["create_time"] = date("Y-m-d H:i:s",$d["create_time"]);
			}
			$data = $this->view->jqflexgrid($data,"id");
		    $this->_helper->json($data); 
        }else{
        	//获取页面
    		$this->view->title = "合同附件列表";
        }
    }
    
    /**
     * 根据合同id获取附件列表
     */
    public function listbycontractAction()
    {
    	$contractId = intval($this->getRequest()->getParam("contractid"));
    	
    	//获取数据
    	$userM  = new Application_Model_DbTable_User();	    	
    	$userKV = $userM->getKVnameid();
    	
    	$contractAttachmentM = new Application_Model_DbTable_Contractattachment();
    	//$data = $contractAttachmentM->fetchAll();
    	$pageM = $this->_helper->Flexgp();
		$data = $contractAttachmentM->jqfetchAll($pageM," `contract_id` = {$contractId} ");
    	$rdata= $data["data"];
		foreach ($rdata as &$d){
			$d["create_user"] = $userKV[$d["create_user"]];
			$d["create_time"] = date("Y-m-d H:i:s",$d["create_time"]);
		}
		$data = $this->view->jqflexgrid($data,"id");
		$this->_helper->json($data);
    }
    
    /**
     * 添加
     */
    public function addAction()
    {
    	$contractId = intval($this->getRequest()->getParam("contractid"));
    	
    	$this->view->title = "新增合同附件";
    	$formM = new Application_Form_Base("contract_attachment");
    	$contractAttachmentM = new Application_Model_DbTable_Contractattachment();
    	
    	$formM->setAction($this->view->pagePath . "/contractattachment/add");
    	$contractE = $formM->getElement("contract_id");
    	$contractE->setValue($contractId);
    	
    	if($this->getRequest()->isPost()){
        	//提交数据
        	//json返回的数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		$contractAttachmentM = new Application_Model_DbTable_Contractattachment();
        		$data  = $formM->getValues(); 
        		//$data["id"] = $data["userid"];unset($data["userid"]);
        		$data['create_user'] = $this->_user_id;	//创建人id
	        	$data['create_time'] = time(); 			//创建时间
        		$data['temp_state']  = 1; 				//临时
        		
	        	try {
		        	$result = $contractAttachmentM->insert($data);
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
		        	Application_Service_Msg::write(
		        		"{$this->_user_name}添加合同附件：{$data["name"]}",
		        		Application_Model_Msg::TYPE_USERDO_LOG
		        	);
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
        }else{
    		$this->view->form = $formM;
        }
        $this->_helper->layout()->disableLayout();
        $this->renderScript("zendform.phtml");
    }
    
    /**
     * 编辑
     * @see LoginController::indexAction()
     */
    public function editAction()
    {
    	$id = intval($this->getRequest()->getParam("id",0));
    	$contractId = intval($this->getRequest()->getParam("contractid"));
    	
    	$this->view->title = "编辑合同附件";
    	
    	$formM = new Application_Form_Base("contract_attachment");
    	$formM->setAttrib("id", "Contractattachment_form");
    	$contractAttachmentM = new Application_Model_DbTable_Contractattachment();
    	
        $formM->setAction($this->view->pagePath . "/contractattachment/edit");
    	
    	if($this->getRequest()->isPost()){
        	//提交数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		
        		$data  = $formM->getValues(); 
        		unset($data["userid"]);
        		$data["temp_state"] = 0;
	        	try {
		        	$result = $contractAttachmentM->update($data, " `id` = {$id} ");
		        	$reData["status"] = 1;
		        	$reData["approve_status"] = "0"; //变成未审核状态
		        	$reData["msg"]="成功！";
		        	Application_Service_Msg::write(
		        		"{$this->_user_name} 修改合同附件：{$data["id"]}",
		        		Application_Model_Msg::TYPE_USERDO_LOG
		        	);
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
        }else{
        	//获取表单
			$data = $contractAttachmentM->fetchRow(" `id` = {$id} ");
        	if($data){
        		$formM->setDefaults($data->toArray());
        	}
			$this->view->form = $formM;
        }
		$this->_helper->layout()->disableLayout();
        $this->renderScript("zendform.phtml");
    }
    
	/**
	 * 删除
	 */
	public function delAction()
	{
		$reData = array(
			"status" => 0,
			"msg"    => "参数错误"
		);	    
    	$id = (array)$this->getRequest()->getParam('id');
    	if($id){
			$contractAttachmentM = new Application_Model_DbTable_Contractattachment();
			$where = $contractAttachmentM->getAdapter()->quoteInto('`id` IN(?)', $id);
    		if($result = $result = $contractAttachmentM->delete($where)){
    			$reData = array(
					"status" => 1,
    		 		"msg"    => "成功删除{$result}条数据！"
    		 	);
    		 	$id = implode(",",$id);
    		 	Application_Service_Msg::write(
	        		"{$this->_user_name} 删除合同附件：{$id}",
	        		Application_Model_Msg::TYPE_USERDO_LOG
	        	);
    		 }
	    }else{
	        	
	    }
	    $this->_helper->json($reData);
   }
}

